﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netra
{
    class Merch
    {
        static private Random random = new Random();

        private string name;

        private int buyprice;
        public int BuyPrice { get { return buyprice; } }

        private int sellprice;
        public int SellPrice { get { return sellprice; } }

        private int basequantity;
        public int BaseQuantity { get { return basequantity; } }

        private int occurrencerate;
        public int OccurrenceRate { get { return occurrencerate; } }

        public enum EType
        {
            Raw=0,
            Processed=1
        }

        private EType type;
        public EType Type { get { return type; } }




        public static List<Merch> Goods = new List<Merch>();

        public static Merch GoodsByName_slow(string name)
        {
            var found = from g in Goods where g.Name==name select g;
            return found.First();            
        }

        public static Merch TossWareToProduce()
        {
            int sum = Goods.Select(x => x.OccurrenceRate).Sum();
            int toss = random.Next(1, sum);
            foreach (Merch ware in Goods)
            {
                toss -= ware.OccurrenceRate;
                if (toss <= 0)
                    return ware;
            }
            throw new ArgumentException("Problem with TossWareToProduct: probably Goods list empty");
        }

        public static Merch TossWareToDemand()
        {
            int sum = Goods.Count();
            int toss = random.Next(1, sum);
            foreach (Merch ware in Goods)
            {
                toss -= 1;
                if (toss <= 0)
                    return ware;
            }
            throw new ArgumentException("Problem with TossWareToDemand: probably Goods list empty");
        }

        public string Name 
        {
            get
            {
                return name;
            }
        }


        public Merch(string pnazwa)
        {
            name = pnazwa;
        }

        public Merch(string Name,int BuyPrice,int SellPrice,int BaseQuantity, int OccurrenceRate, EType Type)
        {
            name = Name;
            buyprice = BuyPrice;
            sellprice = SellPrice;
            basequantity = BaseQuantity;
            occurrencerate = OccurrenceRate;
            type = Type;
        }

        public override string ToString()
        {
            return name+" buy:"+BuyPrice.ToString()+" sell:"+SellPrice.ToString()+" quantity:"+BaseQuantity.ToString()+" occur:"+OccurrenceRate.ToString();
        }
    }
}
